package gizwits.overseaair.way.controldevice;

import java.security.MessageDigest;

import org.apache.commons.lang.ArrayUtils;

import gizwits.kit.ByteUtil;

public class Test2 {
	
	static byte[] cmdCode = new byte[]{(byte)0x90, 0x03};

	public static void main(String[] args) {
//		StringBuffer sb = new StringBuffer("");
//		sb.append("aa").append(",").append("bbb");
//		System.out.println(sb.toString());
//		System.out.println(System.currentTimeMillis());
		
//		String s = "59BAAC310A58462BB0C515C4A0751B9A";
//		ByteUtil.printBytes(ByteUtil.hexStringToBytes(s));
//		
//		ByteUtil.printBytes(genSigned(ByteUtil.concatAll(new byte[]{(byte)0x90,0x03}, new byte[]{(byte)0xa0,0x75,0x1b,(byte)0x9a})));
//		
//		System.out.println(UUID.randomUUID().toString());
		
		byte[] secret = hexStringToBytes("2704E39401000110");
		byte[] mark = hexStringToBytes("1C000490DC000000520C1C5950522C1A01000110000000000E26B6AB");
		
		byte[] did = arrayCopy(mark, 12, 8);
		
		System.out.println(ByteUtil.printHexString(genSigneds(arrayAddAll(reverse(cmdCode), did))));
		
		byte[] dsecret = genSigneds(arrayAddAll(reverse(cmdCode), did));
		
		
//		mark = arrayCopy(mark,20,4);
//		System.out.println(ByteUtil.toInt(mark,0,mark.length));
		
		byte[] baseB = arrayCopy(mark,0,24);
		long signLong = getSignData(genSigneds(arrayAddAll(baseB, dsecret)));
		byte[] signB = toByteOfLittle(signLong,4);
		System.out.println(ByteUtil.printHexString(arrayAddAll(signB)));
		//System.arraycopy(ByteUtil.hexStringToBytes(), start, result, 0, length);
	}
	
	/**
	 * 【小端格式】【数字】转【字节数组】
	 */
	public static byte[] toByteOfLittle(long num,int len){
		byte[] bs = new byte[len];
		for(int i=0;i<len;i++){
			bs[i] = (byte)(0xff & (num >> (i*8)));
		}
		return bs;
	}
	
	/**
	 * 【小端格式的字节数组】转【数字】
	 */
	public static long toLongOfLittle(byte[] bs,int start){
		long num = 0;
		for(int i=0;i<bs.length;i++){
			num = num | ((bs[i+start] & 0xff) << i * 8);
		}
	    return num;  
	}
	
	/**
     * 拆分MD5签名16字节再相加
     * @param sign
     * @return
     */
    public static long getSignData(byte[] sign){
    	long sign1 = toLongOfLittle(arrayCopy(sign, 0, 4),0);
		long sign2 = toLongOfLittle(arrayCopy(sign, 4, 4),0);
		long sign3 = toLongOfLittle(arrayCopy(sign, 8, 4),0);
		long sign4 = toLongOfLittle(arrayCopy(sign, 12, 4),0);
		return sign1+sign2+sign3+sign4;
    }
	
	/**  
	    * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序，和和intToBytes（）配套使用 
	    *   
	    * @param src  
	    *            byte数组  
	    * @param offset  
	    *            从数组的第offset位开始  
	    * @return int数值  
	    */    
	public static int bytesToInt(byte[] src, int offset) {  
	    int value;    
	    value = (int) ((src[offset] & 0xFF)   
	            | ((src[offset+1] & 0xFF)<<8)   
	            | ((src[offset+2] & 0xFF)<<16)   
	            | ((src[offset+3] & 0xFF)<<24));  
	    return value;  
	}  
	
	/**
	 * byte数组深复制
	 * @param source source
	 * @param start source 的开始，从0开始算
	 * @param length 截取source的长度
	 * @return
	 */
	public static byte[] arrayCopy(byte[] source, int start, int length){
		byte[] result = new byte[length];
		//printHexString(source);
		System.arraycopy(source, start, result, 0, length);
		//printHexString(result);
		return result;
	}
	
	/***
	 * 将byte数组翻转
	 * @param validData
	 * @return
	 */
	public static byte[] reverse(byte[] validData){
		ArrayUtils.reverse(validData);
		return validData;
	}
	
	/** 
	    * Convert char to byte 
	    * @param c char 
	    * @return byte 
	    */  
	    private static byte charToByte(char c) {  
	       return (byte) "0123456789ABCDEF".indexOf(c);  
	   }
	
	public static byte[] hexStringToBytes(String hexString) {
	       if (hexString == null || hexString.equals("")) {  
	           return null;  
	       }  
	       hexString = hexString.toUpperCase();  
	       int length = hexString.length() / 2;  
	       char[] hexChars = hexString.toCharArray();  
	       byte[] d = new byte[length];  
	       for (int i = 0; i < length; i++) {  
	           int pos = i * 2;  
	           d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
	       }  
	       return d;  
	   }
	
	public static byte[] genSigned(byte[] source){
		byte[] dsecret = new byte[16];
		try{
			dsecret = MessageDigest.getInstance("MD5").digest(source);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dsecret;
	}

	/***
	 * 组合byte数组
	 * @param src src
	 * @param bs byte array array
	 * @return
	 */
	public static byte[] arrayAddAll(byte[]... bs){
		byte[] result = bs[0];
		for(int i=1;i<bs.length;i++){
			result = ArrayUtils.addAll(result, bs[i]);
		}
		return result;
	}

	
	/**
	 * 根据source生产签名
	 * @param source
	 * @return
	 */
	public static byte[] genSigneds(byte[] source){
		byte[] dsecret = new byte[16];
		try{
			dsecret = MessageDigest.getInstance("MD5").digest(source);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dsecret;
	}
}
